﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SkyBase
{
    class HealthTile:GameObject
    {
        private int hpTick = 60;
        private int hpPerTick;
        private int maxHP;
        private bool depleted = false;

        private Random random;

        public HealthTile(Vector2 locationIn, World worldIn, Random r)
            : base("healthTile", locationIn, Storage.getTexture("HealthTile"), false, worldIn)
        {
            checkForCollision = true;
            ghost = true;
            random = r;

            hpPerTick = random.Next(2, 9+World.Floor);
            maxHP = random.Next(hpPerTick * 2, hpPerTick * 10);
            Debugger.enabled = true;
        }

        public override void onCollision(GameObject collidee)
        {
            if (collidee is Player && !(collidee is Enemy))
            {
                Player p = (Player)collidee;
                if (hpTick == 60 && maxHP > 0)
                {
                    if (p.Health <= p.maxHealth - hpPerTick)
                        p.Health += hpPerTick;
                    else
                        p.Health = p.maxHealth;
                    hpTick = -1;
                    maxHP -= hpPerTick;
                }
                hpTick++;
            }
        }

        public override void Update()
        {
            base.Update();
            if (!depleted && maxHP <= 0)
            {
                Sprite = Storage.getTexture("HealthTileDead");
                depleted = true;
                Debugger.writeLine("Depleted!");
            }
        }
    }
}
